package com.dyh.quickdemozwei.hxfz;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class TestZz {

    SimpleDateFormat sdf0 = new SimpleDateFormat(
//                "yyyy-MM-dd HH:mm:ss"
            "yyyy-MM-dd 00:00:00"
    );

    @Test
    public void test(){
        String str = "{\"result\":\"淘宝平台异常\",\"code\":\"api_exception\",\"message\":\"淘宝平台异常\"}\n";
        String pattern = "\"result\":\"(.*?)\"";

        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(str);

        if (m.find()){
            System.out.println(m.group(1));
        }
        System.out.println();
    }

    @Test
    public void test2(){
        String str = "平台响应报文：{\"error_response\":{\"code\":15,\"msg\":\"Remote service error\",\"sub_code\":\"isv.item-quantity-item-update-service-error-tmall\",\"sub_msg\":\"IC_RULE_ERR_DPC_PRODUCT_CANNOT_MODIFY\",\"request_id\":\"scso4pn7gcds\"}}\t";
        String pattern = "\"sub_msg\":\"(.*?)\"";

        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(str);

        if (m.find()){
            System.out.println(m.group(1));
        }
        System.out.println();
    }

    @Test
    public void test3(){
        String str =
                "{\"result\":\"淘宝平台修改失败\",\"code\":\"isv.ic_rule_tmallcampaign_100037334_1_ku_cun_lock_message\",\"message\":\"Remote service error-该商品为#2023年天猫99超值购#活动商品，当前时间段不允许做修改商品库存或增删sku操作\"}";

        String pattern = "\"message\":\"(.*?)\"";

        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(str);

        if (m.find()){
            System.out.println(m.group(1));
        }
        System.out.println();
    }

    @Test public void test4(){
        Pattern[] patterns = {
                Pattern.compile("\"message\":\"(.*?)\""),
                Pattern.compile("\"sub_msg\":\"(.*?)\""),
        };

        String str = "平台响应报文：{\"error_response\":{\"code\":15,\"msg\":\"Remote service error\",\"sub_code\":\"isv.item-quantity-item-update-service-error-tmall\",\"sub_msg\":\"IC_RULE_ERR_DPC_PRODUCT_CANNOT_MODIFY\",\"request_id\":\"scso4pn7gcds\"}}\t";

//        String str = "{\"result\":\"淘宝平台修改失败\",\"code\":\"isv.ic_rule_tmallcampaign_100037334_1_ku_cun_lock_message\",\"message\":\"Remote service error-该商品为#2023年天猫99超值购#活动商品，当前时间段不允许做修改商品库存或增删sku操作\"}";

        String re = "";
        for (Pattern pattern : patterns) {
            Matcher m = pattern.matcher(str);
            if (m.find()){
                re = m.group(1);
                break;
            }
        }

        System.out.println(re);
    }

    @Test public void testxx(){
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(Calendar.MONTH, -1); // 减去一个月
        calendar.set(Calendar.DAY_OF_MONTH, 1); // 设置日期为1号
        Date firstDayOfLastMonth = calendar.getTime();


        System.out.println(sdf0.format(now));
        System.out.println(sdf0.format(firstDayOfLastMonth));


//        IntFunction

        System.out.println();
    }


    @Test public void testXX(){
        String str = "[{\"skuId\":1,\"skuName\":\"x1\"},{\"skuId\":2,\"skuName\":\"x2\"}]\n";

        List<JSONObject> res = JSON.parseArray(str,JSONObject.class);

        List<List> col = collect(res,e->e.getLong("skuId"),e->e.getString("skuName"));

        String sql = String.format("select * from oms.order where order_id in (%s);",
                col.get(0).stream().map(String::valueOf)
                .collect(Collectors.joining(","))
        );

        System.out.println(sql);



    }



    @Test public void testXh(){
        String str = "{\"result\":\"淘宝平台修改失败\",\"code\":\"isv.ic_rule_tmallcampaign_100037334_1_ku_cun_lock_message\",\"message\":\"Remote service error-该商品为#2023年天猫99超值购#活动商品，当前时间段不允许做修改商品库存或增删sku操作\"}\n";

        JSONObject jo = new JSONObject();
        jo.put("xx",str);

        System.out.println(JSON.toJSONString(jo));

    }


    @Test public void testYY(){
        String str = "[{\"skuId\":1,\"skuName\":\"x1\"},{\"skuId\":2,\"skuName\":\"x2\"}]\n";

        List<JSONObject> res = JSON.parseArray(str,JSONObject.class);

       Set col = collect(res,e->e.getLong("skuId"));

        String sql = String.format("select * from oms.order where order_id in (%s);",
                col.stream().map(String::valueOf)
                        .collect(Collectors.joining(","))
        );

        System.out.println(sql);



    }


    <E> List collect(Collection<E> col, Function<E,?>... fc)  {
        List[] listArr = new List[fc.length];

        for (int i = 0; i < fc.length; i++) {
            listArr[i] = new ArrayList();
        }

        for (E o : col) {
            for (int i = 0; i < fc.length; i++) {
                listArr[i].add(fc[i].apply(o));
            }

        }

        return Arrays.asList(listArr);
    }

      <E> Set collect(Collection<E> col, Function<E,?> fc)  {
        Set set = new HashSet();
        for (E o : col) {
            set.add(fc.apply(o));
        }

        return set;
    }

//    <E> List collect(Collection<E> col, Function<E,?>... fc)  {
//        List<List> listArr = new ArrayList<>();
//
//        for (int i = 0; i < fc.length; i++) {
//            listArr.add(new ArrayList());
//        }
//
//        for (E o : col) {
//            for (int i = 0; i < fc.length; i++) {
//                listArr.get(i).add(fc[i].apply(o));
//            }
//
//        }
//
//        return Arrays.asList(listArr);
//    }


}
